package doc.ic.walkmore.toolbox;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map.Entry;
import java.util.AbstractMap.SimpleEntry;

public class ToolBox {

	public static final double MOVEMENT_THRESHOLD = 1.0;
	
	// How often to take sample. Every 50ms
	public static final long SAMPLING_PERIOD_MS = 50;
	
	// @attribute class {walking, running, up-stairs, down-stairs}
	public static final String[] classes = {"walking", "running", "up-stairs", "down-stairs"};
	
	/**
	 * Finds the max of an positive array (postive values)
	 * @param array
	 * @return an entry index_max -> max
	 */
	public static Entry<Integer, Double> max(double[] array) {
		double max = 0.0; int i_max=-1;
		for (int i=0; i<array.length; i++) {
			if (array[i]>max) {
				max = array[i];
				i_max = i;
			}
		}
		
		return new SimpleEntry<Integer, Double>(i_max,max);
	}
	
	public static  String timestampToStringDate(long timestamp, String format) {
		Date date = new Date(timestamp);
		
		DateFormat df = new SimpleDateFormat(format);
		String textDate = df.format(date);
		
		return textDate;
	}
	
	/**
	 * Smoothing algorithm using the weighted sliding average technique. Which consists of sliding a window 
	 * which computes the average of the neighbors. Precondition : window.length is odd. 
	 * @param values on which to apply the algorithm
	 * @param window
	 * @return the smoothed values
	 */
	public static final double[] slidingAverage(double[] newValues, double[] window) {
		int n =newValues.length;
		double[] newVs = new double[n];
		double avg = 0.0, sum = 0.0;
		for(int i=0;i<n;i++){
			avg=0.0;
			sum=0.0;
			for(int j=0;j<window.length;j++){
				int index = i-(window.length/2)+j;
				if (index>=0 && index < n) {
					avg += window[j]*newValues[index];
					sum += window[j];
				}
			}
			newVs[i] = avg/sum;
		}
		return newVs;
	}
	
 
}
